package cn.jjxx.modules.iface.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jjxx.core.common.controller.BaseController;
import cn.jjxx.core.query.wrapper.EntityWrapper;
import cn.jjxx.core.utils.StringUtils;
import cn.jjxx.modules.activiti.service.IFlowService;
import cn.jjxx.modules.activiti.workflow.BillBean;
import cn.jjxx.modules.activiti.workflow.Config;
import cn.jjxx.modules.oa.entity.OaNotification;
import cn.jjxx.modules.oa.service.IOaNotificationService;
import cn.jjxx.modules.sys.entity.Organization;
import cn.jjxx.modules.sys.entity.User;
import cn.jjxx.modules.sys.service.IOrganizationService;
import cn.jjxx.modules.sys.service.IUserService;
import cn.jjxx.modules.sys.utils.UserUtils;
import cn.jjxx.modules.workflow.entity.Task;
import cn.jjxx.modules.workflow.service.ITaskService;

@Controller
@RequestMapping("/iface/web/home")
@Api(tags = "PC首页接口",description="PC首页接口说明",hidden=true)
public class WebHomeController{
	
	@Autowired
	protected IOaNotificationService notificationService;
	@Autowired
    protected IFlowService flowService;
	@Autowired
    protected ITaskService taskService;
	@Autowired 
	protected IOrganizationService organizationService;
	@Autowired 
	protected IUserService userService;

	/**
	 * @Description: 获取通知信息的最新10条数据 .<br>
	 * @author 郑成功 .<br>
	 * @date 2018-7-4 上午8:59:29.<br>
	 */
	@RequestMapping(value = "/findNotifyList",method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	@ApiOperation(value="获取首页通知",notes="获取首页最新的10条通知")
	@ApiResponse(response=Organization.class, code = 200, message = "接口返回对象参数")
	public List<Map<String,Object>> findNotifyList(){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		EntityWrapper<OaNotification> wrapper = new EntityWrapper<OaNotification>(OaNotification.class);
		wrapper.setSqlSelect("title,content,date_format(create_date,'%Y-%m-%d') as createDate ");
		wrapper.orderBy("createDate",false);
		list = notificationService.selectMaps(wrapper);
		if(list.size()>9){
			return list.subList(0, 10);
		}
		return list;
	}
	
	/**
	 * @Description: 获取当前登录人的代办流程 .<br>
	 * @author 郑成功 .<br>
	 * @date 2018-7-4 上午8:59:29.<br>
	 */
	@RequestMapping(value = "/findMyTaskList",method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	@ApiOperation(value="获取代办任务",notes="获取当前登录人的代办任务",httpMethod="GET")
	@ApiResponse(response=Organization.class, code = 200, message = "接口返回对象参数")
	public List<Map<String,Object>> findMyTaskList(){
		String taskUrl = "activiti/flow/flowAudit?taskId=";
		String signUrl = "activiti/flow/personal/needDisposeTask";
		String meterTaskUrl = "meter/billreview/{billId}/taskinfo";
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		EntityWrapper<Task> wrapper = new EntityWrapper<Task>(Task.class);
		wrapper.eq("assignee", UserUtils.getUser().getId());
		wrapper.or("signer = '"+UserUtils.getUser().getId()+"'");
		wrapper.orderBy("create_time_", false);
		List<User> users = userService.selectList(null);
		List<Task> tasks = taskService.selectList(wrapper);
		for(Task task:tasks){
			Map<String,Object> map = new HashMap<String, Object>();
			Map<String,Object> variables = flowService.getRuVariables(task.getExecutionId());
			BillBean billBean = (BillBean) variables.get(Config.variable.表单信息.toString());
			String submitName = UserUtils.getUser(billBean.getSubmitter(), users).getRealname();
			map.put("taskId", task.getId());
			map.put("type", task.getFlowType());
			map.put("business", task.getBelongBusiness());
			map.put("name", task.getFlowName());
			map.put("nodeName", task.getName()); 
			map.put("treatmentType", task.getTreatmentType());
			map.put("noticeTemplet", billBean.getNoticeTemplet());
			map.put("url", taskUrl+task.getId());
			if(task.getTreatmentType().equals(String.valueOf(1))){
				map.put("url", StringUtils.isNotEmpty(billBean.getTaskSignUrl())?billBean.getTaskSignUrl():signUrl);
			}else{
				if(StringUtils.isNotEmpty(billBean.getTaskAuditeUrl())&&billBean.getTaskAuditeUrl().contains("{billId}")){
					map.put("url", billBean.getTaskAuditeUrl().replace("{billId}", task.getBusinessKey()));
				}else if(StringUtils.isNotEmpty(billBean.getTaskAuditeUrl())){
					map.put("url", billBean.getTaskAuditeUrl()+task.getId());
				}else{
					if(task.getFlowName().contains("计量")||task.getFlowType().contains("计量")){
						map.put("url", meterTaskUrl.replace("{billId}", task.getBusinessKey()));
					}
				}
			}
			variables.put("submitName", submitName);
			variables.remove(Config.variable.表单信息.toString());
			map.put("billInfo", variables);
			list.add(map);
		}
		return list;
	}
}
